<?php 
	$formCommand = $this->formCommand ? $this->formCommand : 'createRequest';
?>
<div class="grid_15" id="newStyles">
<script language="javascript">
	var loanPeriods = <?php $this->getDisplayer()->renderLibraryLoanPeriodJson(); ?>;
	
	function changeLibraryLoanPeriods() {
		var currentLib = $("#requestLocation option:selected").val();
		
		if(loanPeriods[currentLib] && loanPeriods[currentLib].length) {
			//jQuery("#requestPeriodLabel").hide();
			$("#requestPeriodLabel").show();
			$("#requestPeriodLabel option").remove();
			for (i = 0; i < loanPeriods[currentLib].length; i++) {
				var currentLoanPeriod = loanPeriods[currentLib][i].loan_period;
				var currentDefaultStatus = loanPeriods[currentLib][i]['default'];
				$('#requestPeriodLabel select').append('<option value="' + currentLoanPeriod + '">' + currentLoanPeriod + '</option>');
				//jQuery("#loanPeriodSelector option[value=" + currentLoanPeriod + "]").show();
				//if (currentDefaultStatus == "true") {
				//	jQuery("#loanPeriodSelector option[value=" + currentLoanPeriod + "]").attr("selected","selected");
				//}
			}
			//if (loanPeriods[currentLib].length > 1) {
			//	$("#requestPeriodLabel").show();
			//}
		} else {
			$("#requestPeriodLabel").hide();
		}
	}

	function validateField(thisfield) {
		var val=jQuery(thisfield).val();
		if(val == null || val == false || val == "" ) {
			return false;
		}
		else {
			return true;
		}
	}

	function parseDate(inputString) {
		var dateStringArray = inputString.split("-");
		var dateIntArray = new Array();
		for (var i = 0; i < dateStringArray.length; i++) {
			dateIntArray[i] = parseInt(dateStringArray[i], 10);
		}
		var dateOutput = new Date();
		dateOutput.setFullYear(dateIntArray[0]);
		dateOutput.setMonth(dateIntArray[1] - 1);
		dateOutput.setDate(dateIntArray[2]);
		return dateOutput;
	}
	
	function validateRequestForm() {
		var validate=true;
		var error;
		var validateCreateList= new Array(
			'[name="requestType"] option:selected', 
			'[name="itemGroup"]:checked',
			'[name="loanPeriod"] option:selected',
			'[name="title"]'
		);

		var barcodeValue = $('#barcodeSearchField').val();
		var controlNumberValue = $('#controlNumberSearchField').val();
		if (barcodeValue || controlNumberValue) {
			return true;
		}

		// Check each required form field and make sure it's populated.
		for (var i = 0; i<validateCreateList.length; i++) {
			if (validate) {
				validate = validateField(validateCreateList[i]);
			}
			if(!validate) {
				alert("Please ensure that all required fields are filled in.");
				return validate;
				break;
			}
		}

		// make sure we have somethign that even looks like a valid date here.
		var enteredDate = jQuery("#dateNeeded").val();
		
		if (enteredDate.match(/^(?:(?:(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(\/|-|\.)(?:0?2\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\d)?\d{2})(-)(?:(?:(?:0?[13578]|1[02])\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\2(?:0?[1-9]|1\d|2[0-8]))))$/) == null) {
			validate = false;
			alert("Please make sure a valid date is entered.\nProper format is YYYY-MM-DD.");
			return validate;
		}

		// Compare entered date to current and throw error if it's not in the future.
		var dateObj = parseDate(enteredDate);
		var today = new Date();

		if (dateObj <= today) {
			validate = false;
			alert("Please enter a later 'Date Needed By' date.\nProper format is YYYY-MM-DD.");
			return validate;
		}
	}

	jQuery(document).ready(function () {
		changeLibraryLoanPeriods();
		jQuery('#requestLocation').change(function () {
			changeLibraryLoanPeriods();
		});

		$('#requestType').change(function () {
			if ($('#requestTypeLabel select option:selected').val() == 'electronic') {
				$('#requestPeriodLabel select').attr('disabled', 'disabled');
				$('[name="itemGroup"]').attr('disabled', 'disabled');
				$('#requestLocationLabel select').attr('disabled', 'disabled');
				$('#itemGroupGroup').hide();
				$('#requestLocationLabel').hide();
				$('#requestPeriodLabel').hide();
			}
			else {
				$('#requestPeriodLabel select').removeAttr('disabled');
				$('[name="itemGroup"]').removeAttr('disabled');
				$('#requestLocationLabel select').removeAttr('disabled');
				$('#itemGroupGroup').show();
				$('#requestLocationLabel').show();
				$('#requestPeriodLabel').show();				
			}
		});
		$('#requestType').change();
	});	
	</script>
<?php
	if ('' != trim($this->msg)) {?>
	<div  class="helperText"><?php print($this->msg); ?></div>
<?php } ?>

<div class="cancelNavigation">[ <a href="<?php print( $this->returnUrl ? $this->returnUrl : "index.php?cmd={$this->returnCommand}"); ?>">Cancel</a> ]</div>
<h2 class="formHeader grid_7">REQUEST RESERVE FOR COURSE</h2>
<form class="bordered clearing basicPadding bottomMargin" action="./?cmd=createRequest&ci=<?php print(intval($this->ci)); ?>" method="post" name="createRequest" onsubmit="javascript:return validateRequestForm();">
	<input type="hidden" name="cmd" value="<?php print(htmlentities($formCommand)); ?>" />

	<div>You are requesting an item to be put on reserve for:
		<h3 class="noBottomMargin"><?php print($this->course->getTermLabel()); ?> <?php print($this->course->displayCourseNo()); ?></h3>
		<h4 class="noTopMargin"><?php print($this->course->getName()); ?> (<?php print($this->course->getInstructorList()); ?>)</h4>
		Fields marked with an asterisk are required. Providing more information about the item being requested will help expedite processing.
	</div>
	
<?php 

$physicalSelected = Rd_Array::keyExistsAndEquals('requestType', $this->request, 'physical')
	? ' selected="selected" '
	: '';
$electronicSelected = Rd_Array::keyExistsAndEquals('requestType', $this->request, 'electronic')
	? ' selected="selected" '
	: '';
$physicalAndElectronicSelected = Rd_Array::keyExistsAndEquals('requestType', $this->request, 'physAndElec')
	? ' selected="selected" '
	: '';

$multimediaSelected = Rd_Array::keyExistsAndEquals('itemGroup', $this->request, 'MULTIMEDIA')
	? ' checked="checked" '
	: '';
/*$monographSelected = Rd_Array::keyExistsAndEquals('itemGroup', $this->request, 'MONOGRAPH')
	? ' checked="checked" '
	: '';*/
$monographSelected =  !$multimediaSelected
	? ' checked="checked" '
	: '';
$instructorNotesValue = 
	array_key_exists('notes', $this->request)
	? htmlentities($this->request['notes'])
	: '' ;
$datePassed = Rd_Array::keyExistsAndNotBlank('dateNeeded', $this->request) ? $this->request['dateNeeded']  : '';
$dateNeededValue = $this->htmlHelper('valueif', array(
	'' != trim($datePassed), 
	htmlentities($datePassed)
));
$titlePassed = Rd_Array::keyExistsAndNotBlank('title', $this->request) ? $this->request['title']  : '';
$itemTitle = $this->item ? $this->item->getAttribute('title') : '';
$titleFieldValue = $this->htmlHelper('valueif', array(
	'' != trim($titlePassed),
	htmlentities($titlePassed),
	$this->htmlHelper('valueif',array($this->item, $itemTitle))
));
$authorPassed = Rd_Array::keyExistsAndNotBlank('author', $this->request) ? $this->request['author']  : '';
$itemAuthor = $this->item ? $this->item->getAttribute('author') : '';
$authorFieldValue = $this->htmlHelper('valueif',array(
	'' != trim($authorPassed),
	htmlentities($authorPassed),
	$this->htmlHelper('valueif',array($this->item, $itemAuthor))
));
$performerPassed = Rd_Array::keyExistsAndNotBlank('performer', $this->request) ? $this->request['performer']  : '';
$itemPerformer = $this->item ? $this->item->getAttribute('performer') : '';
$performerFieldValue = $this->htmlHelper('valueif',array(
	'' != trim($performerPassed),
	htmlentities($performerPassed),
	$this->htmlHelper('valueif',array($this->item, $itemPerformer))
));
$volumeTitlePassed = Rd_Array::keyExistsAndNotBlank('volume_title', $this->request) ? $this->request['volume_title']  : '';
$itemVolumeTitle = $this->item ? $this->item->getAttribute('volume_title') : '';
$volumeTitleFieldValue = $this->htmlHelper('valueif',array(
	'' != trim($volumeTitlePassed),
	htmlentities($volumeTitlePassed),
	$this->htmlHelper('valueif',array($this->item, $itemVolumeTitle))
));
$volumeEditionPassed = Rd_Array::keyExistsAndNotBlank('volume_edition', $this->request) ? $this->request['volume_edition']  : '';
$itemVolumeEdition = $this->item ? $this->item->getAttribute('edition') : '';
$volumeEditionFieldValue = $this->htmlHelper('valueif',array(
	'' != trim($volumeEditionPassed),
	htmlentities($volumeEditionPassed),
	$this->htmlHelper('valueif',array($this->item, $itemVolumeEdition))
));
$pagesTimesPassed = Rd_Array::keyExistsAndNotBlank('pages_times', $this->request) ? $this->request['pages_times']  : '';
$pagesTimesFieldValue = $this->htmlHelper('valueif',array(
	'' != trim($pagesTimesPassed), 
	htmlentities($pagesTimesPassed)
));
$sourcePassed = Rd_Array::keyExistsAndNotBlank('source', $this->request) ? $this->request['source']  : '';
$itemSource = $this->item ? $this->item->getAttribute('source') : '';
$sourceFieldValue = $this->htmlHelper('valueif',array(
	'' != trim($sourcePassed),
	htmlentities($sourcePassed),
	$this->htmlHelper('valueif',array($this->item, $itemSource))
));
$callNumberPassed = Rd_Array::keyExistsAndNotBlank('call_number', $this->request) ? $this->request['call_number']  : '';
$itemCallNumber = $this->item ? $this->item->getHoldingsAttribute('callNum') : '';
$callNumberFieldValue = $this->htmlHelper('valueif',array(
	'' != $callNumberPassed,
	htmlentities($callNumberPassed),
	$this->htmlHelper('valueif',array($this->item, $itemCallNumber))
));
$isbnPassed = Rd_Array::keyExistsAndNotBlank('isbn', $this->request) ? $this->request['isbn']  : '';
$itemIsbn = $this->item ? $this->item->getAttribute('ISBN') : '';
$isbnFieldValue = $this->htmlHelper('valueif',array(
	'' != trim($isbnPassed),
	htmlentities($isbnPassed),
	$this->htmlHelper('valueif',array($this->item, $itemIsbn))
));
$issnPassed = Rd_Array::keyExistsAndNotBlank('issn', $this->request) ? $this->request['issn']  : '';
$itemIssn = $this->item ? $this->item->getAttribute('ISSN') : '';
$issnFieldValue = $this->htmlHelper('valueif', array(
	'' != $issnPassed,
	htmlentities($issnPassed),
	$this->htmlHelper('valueif', array($this->item, $itemIssn))
));
$oclcPassed = Rd_Array::keyExistsAndNotBlank('oclc', $this->request) ? $this->request['oclc']  : '';
$itemOclc = $this->item ? $this->item->getAttribute('OCLC') : '';
$oclcFieldValue = $this->htmlHelper('valueif', array(
	'' != trim($oclcPassed),
	htmlentities($oclcPassed),
	$this->htmlHelper('valueif', array($this->item, $itemOclc))
));
$urlValue = Rd_Array::keyExistsAndNotBlank('url', $this->request) 
	? $this->request['url']  
	: ($this->item ? $this->item->getAttribute('url') : '');

if (Account_Rd::atLeastStaff()) {
	$barcodeErrorClass = $this->searchBarcodeError ? ' error' : '';
	$controlNumberErrorClass= $this->searchControlNumberError ? ' error' : '';
	$barcodeFieldValue = 
		$this->searchBarcode 
		? ' value="' . htmlentities($this->searchBarcode) . '" ' 
		: '';
	$controlNumberFieldValue = 
		$this->searchControlNumber 
		? ' value="' . htmlentities($this->searchControlNumber) . '" ' 
		: '';
	
	?>
		<div class="grid_6 topMargin">
			<label class="blockLabel bottomHalfMargin<?php print($barcodeErrorClass); ?>">
				<span class="labelText">Barcode: </span><br/>
				<input type="text" name="searchBarcode" id="barcodeSearchField" <?php print($barcodeFieldValue); ?> size="40" /><br/>
				<?php if ($this->searchBarcodeError) { ?>	
					<span class="formAlertMessage supplementText failedText"><?php print($this->searchBarcodeError);?></span>
				<?php } ?>
			</label>			
		</div>
		<div class="prefix_1 grid_6 topMargin">
			<label class="blockLabel bottomHalfMargin<?php print($controlNumberErrorClass); ?>">
				<span class="labelTextExtraLong">Control Number (catkey): </span><br/>
				<input type="text" name="searchControlNumber" id="controlNumberSearchField" <?php print($controlNumberFieldValue); ?> size="40" /><br/>
				<?php if ($this->searchControlNumberError) { ?>	
					<span class="formAlertMessage supplementText failedText"><?php print($this->searchControlNumberError);?></span>
				<?php } ?>
			</label>	
		</div>
		<div class="clear"></div>
		<input class="bottomDoubleMargin" type="submit" name="lookupItemSubmit" value="Item Lookup" />
		
		<div class="clear"></div>
<?php } ?>
		<label class="blockLabel bottomHalfMargin" id="requestTypeLabel"><span class="labelTextLong">Type of Reserve: </span>
			<select name="requestType" id="requestType">
				<option value="physical" <?php print($physicalSelected); ?>>Physical</option>
				<option value="electronic" <?php print($electronicSelected); ?>>Electronic</option>
				<option value="physAndElec" <?php print($physicalAndElectronicSelected); ?>>Both Physical and Electronic</option>
			</select>
		</label>
		<fieldset class="blockFieldset bottomHalfMargin" id="itemGroupGroup">
			<legend class="blockLabel"><span class="labelText">Item Type: </span></legend>
			<label class="labelMarginLong"><input type="radio" name="itemGroup" <?php print($monographSelected); ?> value="MONOGRAPH"> Monograph </label>
			<label><input type="radio" name="itemGroup" <?php print($multimediaSelected); ?> value="MULTIMEDIA"> Multimedia </label>
		</fieldset>	

		<label class="blockLabel bottomHalfMargin" id="requestLocationLabel"><span class="labelTextLong">Reserve Desk: </span>
			<?php $this->getDisplayer()->renderLibrarySelect(array(
				'fieldName' => 'homeLibrary',
				'defaultLibrary' => (
					array_key_exists('homeLibrary', $this->request) 
					? $this->request['homeLibrary'] 
					: $this->course->getPreferredLibrary()->getID()
				)
			)); ?>
		</label>

		<label class="blockLabel bottomHalfMargin" id="requestPeriodLabel"><span class="labelTextExtraLong">Preferred Loan Period: </span>
			<?php $this->getDisplayer()->renderLoanPeriodSelect(array(
				'fieldName' => 'loanPeriod',
				'defaultPeriod' => (
					array_key_exists('loanPeriod', $this->request)
					? $this->request['loanPeriod']
					: ''
				),
				'pickFrom' => (
					array_key_exists('homeLibrary', $this->request) 
					? new library($this->request['homeLibrary'])
					: $this->course->getPreferredLibrary()	
				)	
			));?>
		</label>
		
		<label class="blockLabel topDoubleMargin bottomHalfMargin requiredLabel"><span class="block_2">* Date Needed:</span>
			<input type="text" name="dateNeeded" size="40" type="text" <?php print($dateNeededValue); ?> />
			<span></span>
		</label>
		<script type="text/javascript">
			$( '[name="dateNeeded"]' ).datepicker({
	            showOn: "button",
	            buttonImage: "public/images/icons/calendar-month_sm.png",
	            buttonImageOnly: true,
	            defaultDate: '+2w',
	            dateFormat: "yy-mm-dd",
	            constrainInput: false		            
		    });
		</script>

		<div class="grid_6 topMargin">
			<h3>Item Information:</h3>
			<label class="blockLabel bottomHalfMargin">
				<span class="labelTextExtraLong requiredLabel">* Title: </span><br/>
				<input type="text" name="title" <?php print($titleFieldValue); ?> size="40" />
			</label>	
			<label class="blockLabel bottomHalfMargin">
				<span class="labelTextExtraLong">Author/Composer: </span><br/>
				<input type="text" name="author" <?php print($authorFieldValue); ?> size="40" />
			</label>	
			<label class="blockLabel bottomHalfMargin">
				<span class="labelTextExtraLong">Performer: </span><br/>
				<input type="text" name="performer" <?php print($performerFieldValue); ?> size="40" />
			</label>	
			<label class="blockLabel bottomHalfMargin">
				<span class="labelTextExtraLong">Book/Journal/Work Title: </span><br/>
				<input type="text" name="volume_title" <?php print($volumeTitleFieldValue); ?> size="40" />
			</label>	
			<label class="blockLabel bottomHalfMargin">
				<span class="labelTextExtraLong">Volume/Edition: </span><br/>
				<input type="text" name="volume_edition" <?php print($volumeEditionFieldValue); ?> size="40" />
			</label>	
			<label class="blockLabel bottomHalfMargin">
				<span class="labelTextExtraLong">Pages/Times: </span><br/>
				<input type="text" name="pages_times" <?php print($pagesTimesFieldValue); ?> size="40" />
			</label>	
			<label class="blockLabel bottomHalfMargin">
				<span class="labelTextExtraLong">Source/Year: </span><br/>
				<input type="text" name="source" <?php print($sourceFieldValue); ?> size="40" />
			</label>	
		</div>

		<div class="prefix_1 grid_6 topMargin">
			<h3>Additional Information:</h3>
			<label class="blockLabel bottomHalfMargin">
				<span>Notes for Reserve Staff: </span><br/>
				<textarea name="notes" id="instructorNotesField" cols="40" rows="5"><?php print($instructorNotesValue);?></textarea>
			</label>	
			<label class="blockLabel topDoubleMargin bottomHalfMargin">
				<span class="labelTextExtraLong">Call Number: </span><br/>
				<input type="text" name="call_number" <?php print($callNumberFieldValue); ?> size="40" />
			</label>	
			<label class="blockLabel bottomHalfMargin">
				<span class="labelTextExtraLong">ISBN: </span><br/>
				<input type="text" name="isbn" <?php print($isbnFieldValue); ?> size="40" />
			</label>	
			<label class="blockLabel bottomHalfMargin">
				<span class="labelTextExtraLong">ISSN: </span><br/>
				<input type="text" name="issn" <?php print($issnFieldValue); ?> size="40" />
			</label>	
			<label class="blockLabel bottomHalfMargin">
				<span class="labelTextExtraLong">OCLC#: </span><br/>
				<input type="text" name="oclc" <?php print($oclcFieldValue); ?> size="40" />
			</label>
			<?php if ('' != trim($urlValue)) { ?>
				<input type="hidden" name="url" value="<?php print(htmlentities($urlValue)); ?>"/>
			<?php } ?>		
		</div>
		<div class="clear"></div>
		<input type="hidden" name="referer" value="<?php print($this->returnUrl); ?>"/>
		<input class="topDoubleMargin" type="submit" name="createRequestSubmit" value="Place Request" />
			
			<script language="javascript">

jQuery('[name="ISSN"],[name="ISBN"]').removeAttr('maxlength');
jQuery('[name="ISSN"]').change(itemIdentClean(8));
jQuery('[name="ISBN"]').change(itemIdentClean(13));
function itemIdentClean (maxLength){
	return function(eventObject){
		eventObject.target.value = eventObject.target.value.replace(/[-\W]/g, '').substring(0,maxLength);
	}
}	
			</script>
		</form>
	<div class="clear"></div>
</div>